from playwright.sync_api import sync_playwright, Playwright, expect

def run(p: Playwright):
    chromium = p.chromium  # or "firefox" or "webkit".
    browser = chromium.launch(
        headless=False,
        slow_mo=50,
    )
    context = browser.new_context(
        # storage_state="./output/login.json",
        # viewport={ 'width': 1920, 'height': 1080 }
    )
    page = context.new_page()

    domain = "https://console.volcengine.com/auth"  # coze登录地址
    # redirect_url = "?coze_env=release&new_entry=1&redirectURI=https%3A%2F%2Fsignin.volcengine.com%2Fapi%2Fpassport%2Fsso%2FOIDCAuth%3Fclient_id%3D490362313be302cd4bf07a084b99dbdf%26response_type%3Dcode%26redirect_uri%3Dhttps%253A%252F%252Fsignin.volcengine.com%252Fapi%252Fpassport%252FcloudIdentity%252Fpassport%252Fcallback%26state%3D%25257B%252522platform%252522%25253A%252522eps_cloud_identity%252522%25252C%252522navigatePath%252522%25253A%252522%25252Fhome%252522%25252C%252522type%252522%25253A%252522login%252522%25257D%253D%2526redirect_uri%253Dhttps%25253A%25252F%25252Fsignin-saas.volccloudidentity.com%25252Fapi%25252Fcloudidentity%25252Fauthorize%25252FfederateOAuth%25253Fclient_id%25253D9836804098%252526coze_env%25253Drelease%252526new_entry%25253D1%252526redirect_uri%25253Dhttps%2525253A%2525252F%2525252Fwww.coze.cn%2525252Fauth%2525252Fcallback%252526response_type%25253Dcode%252526scenario%25253Dcoze%252526state%25253D%2525257B%25252522platform%25252522%2525253A%25252522eps_cloud_identity%25252522%2525252C%25252522navigatePath%25252522%2525253A%25252522%2525252Fhome%25252522%2525252C%25252522type%25252522%2525253A%25252522login%25252522%2525257D%2526client_id%253D9836804098%2526scenario%253Dcoze%26scenario%3Dcoze%26utm_source%3Dcoze%26utm_medium%3Dcoze_fusion%26utm_term%3Dcoze_signin%26utm_campaign%3Dfrom_coze%26utm_content%3Dcoze_signin%26new_entry%3D1%26coze_env%3Drelease%26auto_confirm%3D1&scenario=coze&utm_campaign=from_coze&utm_content=coze_signin&utm_medium=coze_fusion&utm_source=coze&utm_term=coze_signin"
    redirect_url = "?coze_env=release&new_entry=1&redirectURI=https%3A%2F%2Fsignin.volcengine.com%2Fapi%2Fpassport%2Fsso%2FOIDCAuth%3Fclient_id%3D490362313be302cd4bf07a084b99dbdf%26response_type%3Dcode%26redirect_uri%3Dhttps%253A%252F%252Fsignin.volcengine.com%252Fapi%252Fpassport%252FcloudIdentity%252Fpassport%252Fcallback%26state%3D%25257B%252522platform%252522%25253A%252522eps_cloud_identity%252522%25252C%252522navigatePath%252522%25253A%252522%25252Fspace%25252F7389079784451522611%25252Fbot%25252F7534566922059530267%252522%25252C%252522type%252522%25253A%252522login%252522%25257D%253D%2526redirect_uri%253Dhttps%25253A%25252F%25252Fsignin-saas.volccloudidentity.com%25252Fapi%25252Fcloudidentity%25252Fauthorize%25252FfederateOAuth%25253Fclient_id%25253D9836804098%252526coze_env%25253Drelease%252526new_entry%25253D1%252526redirect_uri%25253Dhttps%2525253A%2525252F%2525252Fwww.coze.cn%2525252Fauth%2525252Fcallback%252526response_type%25253Dcode%252526scenario%25253Dcoze%252526state%25253D%2525257B%25252522platform%25252522%2525253A%25252522eps_cloud_identity%25252522%2525252C%25252522navigatePath%25252522%2525253A%25252522%2525252Fspace%2525252F7389079784451522611%2525252Fbot%2525252F7534566922059530267%25252522%2525252C%25252522type%25252522%2525253A%25252522login%25252522%2525257D%2526client_id%253D9836804098%2526scenario%253Dcoze%26scenario%3Dcoze%26utm_source%3Dcoze%26utm_medium%3Dcoze_fusion%26utm_term%3Dcoze_signin%26utm_campaign%3Dfrom_coze%26utm_content%3Dcoze_signin%26new_entry%3D1%26coze_env%3Drelease%26auto_confirm%3D1&scenario=coze&utm_campaign=from_coze&utm_content=coze_signin&utm_medium=coze_fusion&utm_source=coze&utm_term=coze_signin"
    page.goto(domain + url + redirect_url)

    # coze登录用户名和密码
    user = "soujony"
    pwd = "Sjm86523652"

    # 点击账号登录
    selector = 'span.arco-tabs-header-title-text:has-text("账号登录")'
    page.click(selector)

    # 登录
    page.get_by_placeholder("请输入账号名/账号ID").fill(user)
    page.get_by_placeholder("请输入登录密码").fill(pwd)
    page.get_by_role("button", name="登录").click()

    # 保存登录文件
    context.storage_state(path="login.json")
    print("保存登录文件成功")


def run1(p: Playwright):
    chromium = p.chromium  # or "firefox" or "webkit".
    browser = chromium.launch(
        headless=False,
        slow_mo=50,
    )
    context = browser.new_context()
    page = context.new_page()

    domain = "http://172.16.12.243"  # dify登录地址243
    url = "/signin"
    page.goto(domain + url)

    # defy登录用户名和密码
    user = "admin@hisetek.com"
    pwd = "Hisetek@123456"

    # 登录
    page.get_by_placeholder("输入邮箱地址").fill(user)
    page.get_by_placeholder("输入密码").fill(pwd)
    page.get_by_role("button", name="登录").click()

    # 等待3秒
    page.wait_for_timeout(3000)

    # 保存登录文件
    context.storage_state(path="./output/login.json")
    print("保存登录文件成功")




with sync_playwright() as p:
    # run(p)
    run1(p)
